package cn.iocoder.yudao.module.biz.service.dangerTicket;

import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import cn.iocoder.yudao.module.biz.controller.admin.dangerTicket.vo.*;
import cn.iocoder.yudao.module.biz.dal.dataobject.dangerTicket.BizDangerTicketDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;

import cn.iocoder.yudao.module.biz.convert.dangerTicket.BizDangerTicketConvert;
import cn.iocoder.yudao.module.biz.dal.mysql.dangerTicket.BizDangerTicketMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.biz.enums.ErrorCodeConstants.*;

/**
 * 危险作业票 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class BizDangerTicketServiceImpl implements BizDangerTicketService {

    @Resource
    private BizDangerTicketMapper dangerTicketMapper;

    @Override
    public String createDangerTicket(BizDangerTicketCreateReqVO createReqVO) {
        // 插入
        BizDangerTicketDO dangerTicket = BizDangerTicketConvert.INSTANCE.convert(createReqVO);
        dangerTicketMapper.insert(dangerTicket);
        // 返回
        return dangerTicket.getId();
    }

    @Override
    public void updateDangerTicket(BizDangerTicketUpdateReqVO updateReqVO) {
        // 校验存在
        this.validateDangerTicketExists(updateReqVO.getId());
        // 更新
        BizDangerTicketDO updateObj = BizDangerTicketConvert.INSTANCE.convert(updateReqVO);
        dangerTicketMapper.updateById(updateObj);
    }

    @Override
    public void deleteDangerTicket(String id) {
        // 校验存在
        this.validateDangerTicketExists(id);
        // 删除
        dangerTicketMapper.deleteById(id);
    }

    private void validateDangerTicketExists(String id) {
        if (dangerTicketMapper.selectById(id) == null) {
            throw exception(DANGER_TICKET_NOT_EXISTS);
        }
    }

    @Override
    public BizDangerTicketDO getDangerTicket(String id) {
        return dangerTicketMapper.selectById(id);
    }

    @Override
    public List<BizDangerTicketDO> getDangerTicketList(Collection<String> ids) {
        return dangerTicketMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<BizDangerTicketDO> getDangerTicketPage(BizDangerTicketPageReqVO pageReqVO) {
        return dangerTicketMapper.selectPage(pageReqVO);
    }

    @Override
    public List<BizDangerTicketDO> getDangerTicketList(BizDangerTicketExportReqVO exportReqVO) {
        return dangerTicketMapper.selectList(exportReqVO);
    }

}
